package com.zhanqiao.controller;

import com.google.gson.Gson;
import com.zhanqiao.service.PublishContentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/publishcontent.do")
public class PublishContentController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String publishDate = req.getParameter("publishdate");
        String state = req.getParameter("state");
//        System.out.println("title="+title+"state="+state+"content="+content+"publishdate="+publishDate);
        String tag = req.getParameter("tag");
        PublishContentService publishContentService = new PublishContentService();
        int i = publishContentService.articleService(title, content, publishDate, state, tag);
        Map message = new HashMap();
        if (i > 0) {
            message.put("msg", "发布成功");
        } else {
            message.put("msg", "发布失败");
        }
        String result = new Gson().toJson(message);
        resp.getWriter().write(result);

    }
}
